<template>
  <el-row :gutter="10" type="flex" justify="center">
    <el-col :xs="24" :sm="18" :md="8" :lg="8">
      <el-form :model="form" :rules="rule" ref="form" label-width="100px">
        <el-form-item>
          <h2>我选网 注册</h2>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input type="text" v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input type="text" v-model="form.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复密码" prop="password_confirmation">
          <el-input type="password" v-model="form.password_confirmation" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>

        <el-form-item>
          <a href="/login">我已经有账号了，去登录。</a><br>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script type="javascript">
  import {errorCallback} from 'resources/assets/js/commons/resource';
  export default {
    data: function () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.password_confirmation !== '') {
            this.$refs.form.validateField('password_confirmation');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          name: '',
          email: '',
          password: '',
          password_confirmation: ''
        },
        rule: {
          name: [
            {required: true, trigger: 'blur'}
          ],
          email: [
            {required: true, type: 'email', trigger: 'blur,change'}
          ],
          password: [
            {validator: validatePass, trigger: 'blur'}
          ],
          password_confirmation: [
            {validator: validatePass2, trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        /**
         * 后台代码请查看 \app\Http\Controllers\Auth\RegisterController.php
         */
        this.$refs[formName].validate(function (valid) {
                  if (valid) {
                    this.$http.post('/register', this.form).then(function (result) {
                      window.location = '/';
                    }).catch(errorCallback);
                  } else {
                    console.log('error submit!!');
                    return false;
                  }
                }.bind(this)
        );
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  };
</script>

<style lang="scss">

</style>
